﻿using System;
using DevExpress.XtraEditors;
using System.Linq;
using System.Windows.Forms;

namespace Management
{
    public partial class frmKullaniciHaklari : XtraForm
    {
        public frmKullaniciHaklari()
        {
            InitializeComponent();
        }

        private void frmKullaniciHaklari_Load(object sender, EventArgs e)
        {

        }

        private void txtKodu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            ShowListe(txtKodu.Text);
        }

        void temizle()
        {
            for (int i = 1; i < vGridControl1.Rows.Count; i++)
            {
                if (vGridControl1.Rows[i].Properties.Value != null)
                {
                    vGridControl1.Rows[i].Properties.Value = string.Empty;
                }
            }
        }

        private void ShowListe(string filtre)
        {
            frmListe liste = new frmListe();
            liste.ListeTipi = frmListe.ListeTipleri.KullaniciListesi;
            liste.SetValueControl = txtKodu;
            if (liste.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string kod = txtKodu.Tag.ToString();
                using (MikroEntities ent = new MikroEntities())
                {
                    Kullanici kul = ent.Kullanicilar.Where(x => x.Kodu == kod).FirstOrDefault();
                    if (kul != null)
                    {
                        txtKodu.Text = kul.Kodu;
                        txtAdi.Text = kul.Adi;
                        var sonuc = ent.KullaniciHaklari.Where(x => x.KullaniciId == kul.ID).ToList();

                        foreach (var item in sonuc)
                        {
                            for (int v = 0; v < vGridControl1.Rows.Count; v++)
                            {
                                if (item.Parametre == vGridControl1.Rows[v].Properties.FieldName)
                                {
                                    switch (vGridControl1.Rows[v].Properties.UnboundType)
                                    {
                                        case DevExpress.Data.UnboundColumnType.Boolean:
                                            vGridControl1.Rows[v].Properties.Value = Convert.ToBoolean(item.Deger);
                                            break;
                                        case DevExpress.Data.UnboundColumnType.DateTime:
                                            vGridControl1.Rows[v].Properties.Value = Convert.ToDateTime(item.Deger);
                                            break;
                                        case DevExpress.Data.UnboundColumnType.Decimal:
                                            vGridControl1.Rows[v].Properties.Value = Convert.ToDecimal(item.Deger);
                                            break;
                                        case DevExpress.Data.UnboundColumnType.Integer:
                                            vGridControl1.Rows[v].Properties.Value = Convert.ToInt32(item.Deger);
                                            break;
                                        case DevExpress.Data.UnboundColumnType.String:
                                            vGridControl1.Rows[v].Properties.Value = Convert.ToString(item.Deger);
                                            break;
                                        default:
                                            vGridControl1.Rows[v].Properties.Value = item.Deger;
                                            break;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        private void txtKodu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
                ShowListe(txtKodu.Text);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            AyarKaydet();
        }

        private void AyarKaydet()
        {
            using (MikroEntities ent = new MikroEntities())
            {
                Kullanici kul = ent.Kullanicilar.Where(x => x.Kodu == txtKodu.Text).FirstOrDefault();
                if (kul != null)
                {
                    string fieldName = string.Empty;
                    for (int i = 1; i < vGridControl1.Rows.Count; i++)
                    {
                        if (vGridControl1.Rows[i].Properties.Value != null)
                        {
                            fieldName = vGridControl1.Rows[i].Properties.FieldName;
                            KullaniciHakki hak = ent.KullaniciHaklari.Where(x => x.KullaniciId == kul.ID && x.Parametre == fieldName).FirstOrDefault();
                            if (hak == null)
                            {
                                ent.AddToKullaniciHaklari(new KullaniciHakki()
                                {
                                    KullaniciId = kul.ID,
                                    Deger = vGridControl1.Rows[i].Properties.Value.ToString(),
                                    Parametre = vGridControl1.Rows[i].Properties.FieldName
                                });
                            }
                            else
                            {
                                hak.Deger = vGridControl1.Rows[i].Properties.Value.ToString();
                            }
                        }
                    }
                }
                ent.SaveChanges();
            }
            MessageBox.Show("Kaydedilmiştir !");
        }

    }
}
